package com.hunan.api.catv.mq.consumer;


import com.google.gson.JsonObject;
import com.hunan.api.catv.mq.MqConstants;
import com.hunan.api.catv.service.biz.subscribe.SubscribeBiz;
import com.hunan.api.catv.utils.GsonUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


@Component
public class SubscribeConsumer {

    public static Logger log = LoggerFactory.getLogger(SubscribeConsumer.class);


    @Autowired
    SubscribeBiz subscribeBiz;

    @RabbitListener(bindings = @QueueBinding(value = @Queue(value = MqConstants.subscribe_queue, durable = "true"), exchange = @Exchange(value = MqConstants.subscribe_exchange, durable = "true", type = "topic"), key = MqConstants.subscribe_routingkey))
    public void submit(String msg) {
        try {
            log.info("[mq收到预约msg：{}]", msg);
            JsonObject msgJson = GsonUtils.fromJson(msg, JsonObject.class);
            Integer flag = msgJson.get("flag").getAsInt();
            Integer memberId = msgJson.get("memberId").getAsInt();
            Integer contentType = msgJson.get("contentType").getAsInt();
            String content = msgJson.get("content").getAsString();
            if (1 == flag) {
                subscribeBiz.save(memberId, contentType, content);
            }
            if (0 == flag) {
                subscribeBiz.cancel(memberId, contentType, content);
            }
        } catch (Exception e) {
            log.error("[mq取消预约失败：{}]", msg);
        }


    }
}